<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// autor/i etapy: Matúš Novák
// 18.11.2013

class Main extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

  //presmeruje na login
	public function index(){
	 $this->load->model('model_main');
		$data['text']=$this->model_main->get_text('welcome_text');
	//	print_r($data['text']);
		$this->template->make_template($data, 'main');
		 //$this->login();
	}
  //zobrazí view login
  public function login(){
     $this->template->make_template("", 'login');
  }
  //zobrazí view signup
  public function signup(){
	$this->template->make_template("", 'signup');
	//$this->load->view('view_signup');
  }
  //zobrazi view kolektiv ak je login


  //zobrazí view members, ak je v session signed up, ináč na view restricted
  public function members(){
    if ($this->session->userdata('is_logged_in')){
        if ($this->session->userdata('admin')){
            redirect('admin/index');
          //$this->load->view('view_admin_main');
        } else {
            redirect('user/index');
          //$this->load->view('view_user_main');
       }
    } else{
       redirect('main/restricted');
    }
  }

  //zobrazí view restricted
  public function restricted(){
     $this->template->make_template("", 'restricted');
  }
  //nastavuje pravidla pre kolonky  a redirectne na members ak je form_validation->run() true
  public function login_validation(){
     $this->load->library('form_validation');

     $this->form_validation->set_rules('email','Email',
     'required|trim|xss_clean|callback_validate_credentials');
     $this->form_validation->set_rules('password','Password','required|md5|trim');

     if ($this->form_validation->run()){
        redirect('main/members');
     } else{
        $this->template->make_template("", 'login');
     }
  }
  //nastaví pravidlá pre kolónky, kontrola správnych údajov registracie + odosielanie mailu
  public function signup_validation(){
      $this->load->library('form_validation');
      // legit email + iba raz v DB
      $this->form_validation->set_rules('skola','Skola','required|trim');
      $this->form_validation->set_rules('email',"Email",'required|trim|valid_email|is_unique[uzivatelia.email]');  //PREPISAT DATABAZU A FIELD
      $this->form_validation->set_rules('password',"Password",'required|trim');
      // ci sedi aj druhy password
      $this->form_validation->set_rules('cpassword',"Confirm Password",'required|trim|matches[password]');

      $this->form_validation->set_message('is_unique', "Email je uz obsadeny, skuste iny");

      if ($this->form_validation->run()){
          //generuje random key
          $key = md5(uniqid());
          //posle email
          $this->load->library('email', array('mailtype'=>'html'));
          $this->load->model('model_main');

          $this->email->from('noreply@PPTV.com', "noreply");
          $this->email->to($this->input->post('email'));
          $this->email->subject("Potvrdenie registracie PPTV.");

          $message = "<p>Dakujeme za registraciu!</p>";
          $message .= "<p><a href='".base_url()."main/register_user/$key' >Kliknite sem</a> na potvrdenie registracie!</p>";

          $this->email->message($message);
          //prida do temp_users databazy
          if ($this->model_main->add_temp_user($key)){
              if ($this->email->send()){
                  echo "Aktivacny email odoslany!";
              } else {
                  echo "Nepodarilo sa odoslat email";
              }
          } else {
              echo "nepodarilo sa pridat do db";
          }
      } else {
          $this->template->make_template("", 'signup');
      }
  }
  //kontrola spravnych prihlasovacich udajov z DB
  public function validate_credentials(){
     $this->load->model('model_main');

     if($this->model_main->can_log_in()){
        $temp = $this->input->post('email');
        $data = $this->model_main->get_user_info($temp);
        if ($data != NULL)
            $this->session->set_userdata($data);
        else return false;
        return true;
     } else {
         $this->form_validation->set_message('validate_credentials',
         'Nespravne meno/heslo');
         return false;
     }
  }
  //	odhlási užívateľa
  public function logout(){
      $this->session->sess_destroy();
      redirect('main/login');
  }
  //	zaregistruje používateľa, ak ešte nie je v DB
  public function register_user($key){
      $this->load->model('model_main');

      if ($this->model_main->is_key_valid($key)){
          if ($this->model_main->add_user($key)){
              $mail = $this->session->userdata('email');
              $tempID = $this->model_main->get_id($mail);
              $this->session->set_userdata('id', $tempID);
              redirect('main/members');
          } else echo "zlyhanie pri pridavani usera";

      } else {
          echo "invalid key";
      }
  }



}
